package _17_剑指Offer;

import java.util.HashMap;
import java.util.Map;

public class _48_剑指Offer最长不含重复字符的子字符串 {

    public static void main(String[] args) {

        _48_剑指Offer最长不含重复字符的子字符串 v = new _48_剑指Offer最长不含重复字符的子字符串();

        String str = "ab";
        System.out.println(v.lengthOfLongestSubstring(str));

    }

    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        int left = 0;
        int right = 0;
        int max = 0;
        // 右指针向右移动
        while (right < chars.length) {
            char curChar = chars[right];
            Integer index = map.get(curChar);
            if (index != null && index >= left) {
                // 左指针移动
                left = index + 1;
            }
            map.put(curChar, right++);
            // 计算最后值
            max = Math.max(max, right - left);
        }
        return max;
    }

}
